package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import com.dingreading.cloud.soms.dto.MemberCashDto;
import com.dingreading.cloud.soms.entity.MemberCash;
import com.dingreading.cloud.soms.entity.Members;
import com.dingreading.cloud.soms.service.MemberCashService;
import com.dingreading.cloud.soms.service.MembersService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/front/memberCash")
@Api(tags = "门店-会员押金管理")
public class FrontMemberCashController {

    @Resource
    private MemberCashService memberCashService;
    @Resource
    private MembersService membersService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "会员姓名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "会员uid") @RequestParam(value = "memberUid", required = false) String memberUid,
            @ApiParam(value = "类型 1.收款 2.退款") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "开始日期") @RequestParam(value = "beginDate", required = false) String beginDate,
            @ApiParam(value = "结束日期") @RequestParam(value = "endDate", required = false) String endDate
    ) throws Exception {
        Page<MemberCashDto> page = memberCashService.pageList(pageUtil, corpUid, storeUid, keyword, memberUid, type, beginDate, endDate);

        PageDto<MemberCashDto> dto = new PageDto<>();
        dto.setList(page.getRecords());
        dto.setTotal(page.getTotalRow());
        return R.ok(dto);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "添加/编辑")
    @PostMapping(value = "/editBean", produces = "application/json; charset=UTF-8")
    public R<Object> editBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "押金uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "会员uid") @RequestParam(value = "memberUid") String memberUid,
            @ApiParam(value = "付款日期") @RequestParam(value = "payDate") String payDate,
            @ApiParam(value = "类型 1.收款 2.退款") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(value = "押金金额") @RequestParam(value = "money") Double money,
            @ApiParam(value = "截图地址") @RequestParam(value = "url") String url
    ) throws Exception {
        if (money == null || money <= 0)
            return R.fail("请填写正确的金额");
        if (StringUtils.isBlank(url))
            return R.fail("请上传截图");
        if (StringUtils.isBlank(payDate))
            return R.fail("请选择付款日期");

        Members members = membersService.getByUid(memberUid);
        if (members == null)
            return R.fail("未查询到会员");

        MemberCash data = null;
        if (StringUtils.isBlank(uid)) {
            data = new MemberCash();
            data.setCorpUid(corpUid);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setMemberUid(memberUid);
            data.setMemberName(members.getMemberName());
            data.setUid(KitUtil.getUuid());
            data.setType(type);
            data.setTypeName(type.equals(1) ? "收款" : "退款");
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
            data.setMoney(money);
            data.setUrl(url);
            data.setPayDate(DateUtil.parseDate(payDate));
            data.setLastStaffUid(staffUid);
            data.setLastStaffName(staffName);
            memberCashService.saveOrUpdate(data);
        } else {
            data = memberCashService.getByUid(storeUid, uid);
            if (data == null)
                return R.fail("获取失败");
            // 判断日期超过30天
            if (TimeUtil.moreThanDay(data.getAddTime(), 3))
                return R.fail("提交超过3天，不能修改");

            memberCashService.updateInfo(storeUid, uid, money, url, payDate, staffUid, staffName);
        }
        return R.ok();
    }

    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public R<Object> get(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "押金uid") @RequestParam(value = "uid") String uid
    ) {
        MemberCashDto data = memberCashService.dtoByUid(storeUid, uid);

        return R.ok(data);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete", produces = "application/json; charset=UTF-8")
    public R<Object> delete(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "押金uid") @RequestParam(value = "uid") String uid
    ) {
        MemberCash data = memberCashService.getByUid(storeUid, uid);
        if (data == null)
            return R.fail("获取失败");
        // 判断日期超过30天
        if (TimeUtil.moreThanDay(data.getAddTime(), 3))
            return R.fail("提交超过3天，不能操作");

        int i = memberCashService.deleteByUid(storeUid, uid);
        if (i <= 0)
            return R.failDel();

        // 删除服务器图片
        LocalFileUtil.deleteImg(LocalFileUtil.memberCastBaseDir, data.getUrl());

        return R.ok();
    }

    @ApiOperation(value = "上传收款/退款截图")
    @PostMapping(value = "/uploadImage", produces = "application/json; charset=UTF-8")
    public R<Object> uploadImage(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工姓名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "图片文件") @RequestPart(value = "file") MultipartFile file
    ) throws Exception {
        // 上传图片到服务器
        R<String> r = LocalFileUtil.uploadImg(LocalFileUtil.memberCastBaseDir, file);
        if (R.isError(r))
            return R.fail(r.getMsg());

        return R.ok(r.getData());
    }

}
